package com.atguigu.eduorder.service.impl;

import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.atguigu.oss.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.oss.commonutils.ordervo.UcenterMemberOrder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author luomingliang
 * @since 2020-07-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    //创建订单, 返回订单号
    @Override
    public String createOrder(String courseId, String memberId) {
        //通过远程调用,根据用户id获取用户信息
        System.out.println("courseId = " + courseId);
        System.out.println("memberId = " + memberId);
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        System.out.println("userInfoOrder = " + userInfoOrder);
        //通过远程调用,根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        System.out.println("courseInfoOrder = " + courseInfoOrder);
        //创建Order对象, 向order对象里面设置所需数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //订单号
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(courseInfoOrder.getTitle()); //课程标题
        order.setCourseCover(courseInfoOrder.getCover()); //课程封面
        order.setTeacherName(courseInfoOrder.getTeacherName()); //讲师姓名
        order.setTotalFee(courseInfoOrder.getPrice()); //课程费用
        order.setMemberId(memberId); //用户id
        order.setMobile(userInfoOrder.getMobile()); //用户手机号
        order.setNickname(userInfoOrder.getNickname()); //用户昵称
        order.setStatus(0); //支付状态,  0:未支付, 1:已支付
        order.setPayType(1); //支付类型, 微信:1, 支付宝: 2

        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
